package ai.mspbots.service.projectb.controller;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

import ai.mspbots.service.projectb.config.Constant;
import ai.mspbots.service.projectb.service.IUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    private static final Logger logger = LogManager.getLogger(UserController.class);
    private AtomicLong counter = new AtomicLong(0);

    @Autowired
    private IUserService userService;

    @RequestMapping("/projectB/{value}")
    public String home(@PathVariable("value") String value) throws InterruptedException {
        if (counter.getAndIncrement() % Constant.SAMPLE_RATE == 0) {
            logger.debug("calling /projectB/{value}");
        }
        Thread.sleep(new Random().nextInt(2) * 1000);
        userService.saveUser(value);
        userService.selectUser(value);
        return value + "-" + UUID.randomUUID().toString();
    }
}
